package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SchoolLinkman;
import cn.wolfcode.crm.query.SchoolLinkmanQueryObject;
import cn.wolfcode.crm.service.ISchoolLinkmanService;
import cn.wolfcode.crm.service.ISchoolService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("linkman")
public class LinkmanController {

    @Autowired
    private ISchoolLinkmanService schoolLinkmanService;
    @Autowired
    private ISchoolService schoolService;


    @RequestMapping("view")
    public String view(){
        return "school_linkman/linkman";
    }


    @ResponseBody
    @RequestMapping("query")
    public PageResult query(SchoolLinkmanQueryObject qo) {
        return schoolLinkmanService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<SchoolLinkman> selectAll() {
        List<SchoolLinkman> schoolLinkmen = schoolLinkmanService.selectAll();
        return schoolLinkmen;
    }

    @RequestMapping("selectBySchoolName")
    @ResponseBody
    public List<SchoolLinkman> selectBySchoolName(String schoolName) {
        Long schoolId = schoolService.selectById(schoolName);
        List<SchoolLinkman> schoolLinkmen = schoolLinkmanService.selectBySchoolName(schoolId);
        return schoolLinkmen;
    }

    @RequestMapping("selectBySchoolId")
    @ResponseBody
    public List<SchoolLinkman> selectBySchoolId() {
        List<SchoolLinkman> schoolLinkmen = schoolLinkmanService.selectAll();
        return schoolLinkmen;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"linkman:saveOrUpdate", "学校联系人增加或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(SchoolLinkman record) {
        try {
            if (record.getId() != null) {
                schoolLinkmanService.updateByPrimaryKey(record);
            } else {
                schoolLinkmanService.insert(record);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"linkman:delete", "学校联系人删除"}, logical = Logical.OR)
    public JSONResult delete(Long id) {
        try {
            schoolLinkmanService.delete(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }



}
